home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / text / misc / mpage.lha / mpage / page.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-25  |  10.5 KB  |  416 lines

  1. /*
  2.  * page.c
  3.  */
  4.  
  5. /*
  6.  * mpage:    a program to reduce pages of print so that several pages
  7.  *           of output appear on one printed page.
  8.  *
  9.  * Written by:
  10.  *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
  11.  *              >pyrdc!mark            Pyramid Technology Corporation
  12.  * ...!pyramid!/                       Vienna, Va    (703)848-2050
  13.  *
  14.  *
  15.  * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
  16.  * Copyright (c) 1994-1997 Marcel J.E. Mol, The Netherlands
  17.  *                    marcel@mesa.nl
  18.  *  
  19.  *     Permission is granted to anyone to make or distribute verbatim
  20.  *     copies of this document as received, in any medium, provided
  21.  *     that this copyright notice is preserved, and that the
  22.  *     distributor grants the recipient permission for further
  23.  *     redistribution as permitted by this notice.
  24.  *
  25.  */
  26.  
  27.  
  28. #include "mpage.h"
  29.  
  30.  
  31.  
  32. void
  33. set_page()
  34. {
  35.     switch (opt_page) {
  36.         case PAGE_A4: /* ISO 216 conforming says 595x841 ... */
  37.                 media = "A4";
  38.         ps_width = 596;        /* 210 mm */
  39.         ps_height = 842;       /* 297 mm */
  40.                 break;
  41.         case PAGE_A3:
  42.                 media = "A3";
  43.         ps_width = 842;        /* 297 mm */
  44.         ps_height = 1192;      /* 420 mm */
  45.                 break;
  46.         case PAGE_LETTER:
  47.                 media = "Letter";
  48.         ps_width = 612;        /* 8.5 inches */
  49.         ps_height = 792;       /* 11 inches */
  50.                 break;
  51.         case PAGE_LEGAL:
  52.                 media = "Legal";
  53.         ps_width = 612;        /* 8.5 inches */
  54.         ps_height = 1008;      /* 14 inches */
  55.                 break;
  56.         default: 
  57.                 fprintf(stderr, "%s: ignoring unknown page format: %d\n",
  58.                                 MPAGE, opt_page);
  59.                 break;
  60.     }
  61.  
  62.     return;
  63.  
  64. } /* set_page */
  65.  
  66.  
  67.  
  68. /*
  69.  * Bases are the base for each page. e.g the corner points for outlines.
  70.  */
  71.    /*  __________________________
  72.     * | ________________________ |
  73.     * ||                        ||
  74.     * ||                        ||
  75.     * ||                 ybase4 ||
  76.     * ||                        Y|
  77.     * ||                        ||
  78.     * ||                        ||
  79.     * ||                 ybase3 ||
  80.     * ||                        Y|
  81.     * ||                        ||
  82.     * ||                        ||
  83.     * ||                 ybase2 ||
  84.     * ||                        Y|
  85.     * ||                        ||
  86.     * ||                        ||
  87.     * ||xbase1   xbase2  ybase1 ||
  88.     * |X___________X____________Y|
  89.     * |__________________________|
  90.     */
  91.  
  92. int
  93. xbase1()
  94. {
  95.     Debug(DB_POINTS, "%%xbase1: %d\n", sheetmargin_left + sheetheader_left);
  96.     return sheetmargin_left + sheetheader_left;
  97. } /* xbase1 */
  98.  
  99.  
  100.  
  101. int
  102. xbase2()
  103. {
  104.     Debug(DB_POINTS, "%%xbase2: %d\n",
  105.                 (ps_width - sheetmargin_left - sheetmargin_right
  106.                           - sheetheader_left - sheetheader_right) / 2 +
  107.                 sheetmargin_left + sheetheader_left);
  108.         return  (ps_width - sheetmargin_left - sheetmargin_right
  109.                           - sheetheader_left - sheetheader_right) / 2 +
  110.                 sheetmargin_left + sheetheader_left;
  111. } /* xbase2 */
  112.  
  113.  
  114.  
  115. int
  116. ybase1()
  117. {
  118.     Debug(DB_POINTS, "%%ybase1: %d\n",
  119.                sheetmargin_bottom + sheetheader_bottom);
  120.     return sheetmargin_bottom + sheetheader_bottom;
  121. } /* ybase1 */
  122.  
  123.  
  124.  
  125. int
  126. ybase2()
  127. {
  128.     Debug(DB_POINTS, "%%ybase2: %d\n",
  129.             (ps_height - sheetmargin_bottom - sheetmargin_top
  130.                            - sheetheader_bottom - sheetheader_top) / 4
  131.             + sheetmargin_bottom + sheetheader_bottom);
  132.     return (ps_height - sheetmargin_bottom - sheetmargin_top
  133.                           - sheetheader_bottom - sheetheader_top) / 4
  134.             + sheetmargin_bottom + sheetheader_bottom;
  135. } /* ybase2 */
  136.  
  137.  
  138.  
  139. int
  140. ybase3()
  141. {
  142.     Debug(DB_POINTS, "%%ybase3: %d\n",
  143.             (ps_height - sheetmargin_bottom - sheetmargin_top
  144.                            - sheetheader_bottom - sheetheader_top) / 2
  145.             + sheetmargin_bottom + sheetheader_bottom);
  146.     return (ps_height - sheetmargin_bottom - sheetmargin_top
  147.                           - sheetheader_bottom - sheetheader_top) / 2
  148.             + sheetmargin_bottom + sheetheader_bottom;
  149. } /* ybase3 */
  150.  
  151.  
  152.  
  153. int
  154. ybase4()
  155. {
  156.     Debug(DB_POINTS, "%%ybase4: %d\n",
  157.             (ps_height - sheetmargin_bottom - sheetmargin_top
  158.                            - sheetheader_bottom - sheetheader_top) * 3 / 4
  159.             + sheetmargin_bottom + sheetheader_bottom);
  160.     return  (ps_height - sheetmargin_bottom - sheetmargin_top
  161.                            - sheetheader_bottom - sheetheader_top) * 3 / 4
  162.             + sheetmargin_bottom + sheetheader_bottom;
  163. } /* ybase4 */
  164.  
  165.  
  166.  
  167. int
  168. ytop1()
  169. {
  170.     Debug(DB_POINTS, "%%ytop1: %d\n", ybase2());
  171.     /* return ybase1() + yht4(); */
  172.     return ybase2();
  173. } /* ytop1 */
  174.  
  175.  
  176.  
  177. int
  178. ytop2()
  179. {
  180.     Debug(DB_POINTS, "%%ytop2: %d\n", ybase3());
  181.     /* return ybase2() + yht4(); */
  182.     return ybase3();
  183. } /* ytop2 */
  184.  
  185.  
  186.  
  187. int
  188. ytop3()
  189. {
  190.     Debug(DB_POINTS, "%%ytop3: %d\n", ybase4());
  191.     /* return ybase3() + yht4(); */
  192.     return ybase4();
  193. } /* ytop3 */
  194.  
  195.  
  196.  
  197. int
  198. ytop4()
  199. {
  200.     Debug(DB_POINTS, "%%ytop4: %d\n", 
  201.            ps_height - sheetmargin_bottom - sheetmargin_top
  202.                      - sheetheader_bottom - sheetheader_top
  203.                      + sheetmargin_bottom + sheetheader_bottom);
  204.     /* return ybase4() + yht4(); */
  205.     return ps_height - sheetmargin_bottom - sheetmargin_top
  206.                      - sheetheader_bottom - sheetheader_top
  207.                      + sheetmargin_bottom + sheetheader_bottom;
  208. } /* ytop4 */
  209.  
  210.  
  211.  
  212. int
  213. xwid1()
  214. {
  215.     Debug(DB_POINTS, "%%xwid1: %d\n",
  216.            ps_width - sheetmargin_left - sheetmargin_right
  217.                     - sheetheader_left - sheetheader_right);
  218.     return ps_width - sheetmargin_left - sheetmargin_right
  219.                     - sheetheader_left - sheetheader_right;
  220. } /* xwid1 */
  221.  
  222.  
  223.  
  224. int
  225. xwid2()
  226. {
  227.     Debug(DB_POINTS, "%%xwid2: %d\n",
  228.            (ps_width - sheetmargin_left - sheetmargin_right
  229.                      - sheetheader_left - sheetheader_right) / 2);
  230.     return (ps_width - sheetmargin_left - sheetmargin_right
  231.                      - sheetheader_left - sheetheader_right) / 2;
  232. } /* xwid2 */
  233.  
  234.  
  235.  
  236. int
  237. yht1()
  238. {
  239.     Debug(DB_POINTS, "%%yht1: %d\n",
  240.            ps_height - sheetmargin_top - sheetmargin_bottom
  241.                      - sheetheader_top - sheetheader_bottom);
  242.     return ps_height - sheetmargin_top - sheetmargin_bottom
  243.                      - sheetheader_top - sheetheader_bottom;
  244. } /* yht1 */
  245.  
  246.  
  247.  
  248. int
  249. yht2()
  250. {
  251.     Debug(DB_POINTS, "%%yht2: %d\n",
  252.            (ps_height - sheetmargin_top - sheetmargin_bottom
  253.                       - sheetheader_top - sheetheader_bottom) / 2);
  254.     return (ps_height - sheetmargin_top - sheetmargin_bottom
  255.                       - sheetheader_top - sheetheader_bottom) / 2;
  256. } /* yht2 */
  257.  
  258.  
  259.  
  260. int
  261. yht4()
  262. {
  263.     Debug(DB_POINTS, "%%yht4: %d\n",
  264.            (ps_height - sheetmargin_top - sheetmargin_bottom
  265.                       - sheetheader_top - sheetheader_bottom) / 4);
  266.     return (ps_height - sheetmargin_top - sheetmargin_bottom
  267.                       - sheetheader_top - sheetheader_bottom) / 4;
  268. } /* yht4 */
  269.  
  270.  
  271.  
  272. void
  273. outline_1(outfd)
  274.  FILE *outfd;
  275. {
  276.     /* one page outline */
  277.     fprintf(outfd, "0 setlinewidth\n");
  278.         fprintf(outfd, "%d %d moveto 0 %d rlineto\n",
  279.                        xbase1(), ybase1(), yht1());
  280.     fprintf(outfd, "%d 0 rlineto 0 %d rlineto closepath stroke\n",
  281.                        xwid1(), -yht1());
  282.  
  283.         return;
  284. } /* outline_1 */
  285.  
  286.  
  287.  
  288. void
  289. outline_2(outfd)
  290.  FILE *outfd;
  291. {
  292.     /* two page outline */
  293.     outline_1(outfd);
  294.     fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
  295.                 xbase1(), ybase3(), xwid1());
  296.  
  297.         return;
  298. } /* outline_2 */
  299.  
  300.  
  301.  
  302. void
  303. outline_4(outfd)
  304.  FILE *outfd;
  305. {
  306.     /* four page outline */
  307.     outline_2(outfd);
  308.     fprintf(outfd, "%d %d moveto 0 %d rlineto stroke\n",
  309.                 xbase2(), ybase1(), yht1());
  310.  
  311.         return;
  312. } /* outline_4 */
  313.  
  314.  
  315.  
  316. void
  317. outline_8(outfd)
  318.  FILE *outfd;
  319. {
  320.     /* eight page outline */
  321.     outline_4(outfd);
  322.     fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
  323.                 xbase1(), ybase2(), xwid1());
  324.     fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
  325.                 xbase1(), ybase4(), xwid1());
  326.  
  327.         return;
  328. } /* outline_8 */
  329.  
  330.  
  331.  
  332. void
  333. mp_outline(outfd, asheet)
  334.  FILE *outfd;
  335.  struct sheet *asheet;
  336. {
  337.  
  338.     if (opt_outline) {
  339.         (*asheet->sh_outline)(outfd);
  340.     }
  341.  
  342.     return;
  343.  
  344. } /* mp_outline */
  345.  
  346.  
  347.  
  348. void
  349. sheetheader(outfd, fname)
  350.  FILE *outfd;
  351.  char *fname;
  352. {
  353.  
  354.     if (opt_sheetheader) {
  355.         fprintf(outfd, "gsave\nheaderfont setfont\n");
  356.         switch (orientation) {
  357.             case PORTRAIT:
  358.                 sheetheader_top = 14;
  359.                 if (*sheethead != '\0') {
  360.                     fprintf(outfd,
  361.                            "%d %d (%s) stringwidth pop sub 2 div add %d moveto",
  362.                             xbase1(), xwid1(), sheethead,
  363.                             ytop4() + fsize / 4);
  364.                     fprintf(outfd, " (%s) show\n", sheethead);
  365.                 }
  366.                 else {
  367.                     /*
  368.                      * Filename on the left
  369.                      */
  370.                     fprintf(outfd, "%d %d moveto (%s) show\n",
  371.                             xbase1() + 3, ytop4() + fsize / 4, fname);
  372.                     /*
  373.                      * Pagenumber on the right
  374.                      */
  375.                     fprintf(outfd,
  376.                             "%d (Page %d) stringwidth pop sub 3 sub %d moveto",
  377.                             xbase1() + xwid1(), ps_pagenum,
  378.                             ytop4() + fsize / 4);
  379.                     fprintf(outfd, " (Page %d) show\n", ps_pagenum);
  380.                 }
  381.                 break;
  382.             case LANDSCAPE:
  383.             case LANDSCAPE_PORTRAIT:
  384.                 sheetheader_right = 14;
  385.                 if (*sheethead != '\0') {
  386.                     fprintf(outfd,
  387.                            "%d %d (%s) stringwidth pop 2 div add moveto",
  388.                             xbase1() + xwid1() + fsize/4,
  389.                             ybase3(), sheethead);
  390.                     fprintf(outfd, " -90 rotate (%s) show\n", sheethead);
  391.                 }
  392.                 else {
  393.                     /*
  394.                      * Filename on the left
  395.                      */
  396.                     fprintf(outfd, "%d %d moveto -90 rotate (%s) show\n",
  397.                             xbase1() + xwid1() + fsize/4, ytop4() - 3, fname);
  398.                     /*
  399.                      * Pagenumber on the right
  400.                      */
  401.                     fprintf(outfd, "90 rotate %d %d (Page %d) stringwidth pop ",
  402.                                    xbase1() + xwid1() + fsize/4,
  403.                                    ybase1() + 3, ps_pagenum);
  404.                     fprintf(outfd, "add moveto -90 rotate (Page %d) show\n",
  405.                                    ps_pagenum);
  406.                 }
  407.                 break;
  408.         }
  409.         fprintf(outfd, "grestore\n");
  410.     }
  411.  
  412.     return;
  413.  
  414. } /* sheetheader */
  415.  
  416.